package org.jeecg.modules.temperature.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.mqtt.MyMQTTClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.xml.bind.DatatypeConverter;
import java.util.LinkedList;
import java.util.Queue;

@Configuration
@EnableScheduling
@Slf4j
public class TemperatureScheduled {

    private final MyMQTTClient myMQTTClient;

    @Value("${mqtt.topic4}")
    String topic4;

    Queue<String> msgQueue = new LinkedList<>();

    public TemperatureScheduled(MyMQTTClient myMQTTClient) {
        this.myMQTTClient = myMQTTClient;
    }

//    @Scheduled(cron = "0/15 * * * * ?")//从0秒开始每15秒执行一次
    @Scheduled(cron = "0 0/2 * * * *")//从0分0秒每2分执行一次
    public void configureTasks() throws Exception{
        String hexString = "0203000400034439";
        byte[] bytes = DatatypeConverter.parseHexBinary(hexString);
        String result = new String(bytes);
        boolean flag = msgQueue.offer(result);
        if (flag) {
            //发布消息  topic2 是你要发送到那个通道里面的主题 比如我要发送到topic2主题消息
            myMQTTClient.publish(msgQueue.poll(), topic4);
        }
    }
}
